package zozopo.user.operations;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.Cookie;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import zozopo.database.BusiDatabaseManager;
import zozopo.database.SqlMachine;
import zozopo.operations.comm.DictionaryOS;
import zozopo.operations.comm.OperatingService;
import zozopo.warehouse.operations.PurchaseTB;

public class TokenOS extends OperatingService{
	
	private static final Log logger = LogFactory.getLog(TokenOS.class);
	
	public TokenOS(Connection conn){
		super(conn);
		
	}
	
	public TokenOS(){
		this.databaseConnection = new BusiDatabaseManager();
		this.conn = databaseConnection.getConnection();
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<TokenTB> selectListBySql(String sql){
		List<TokenTB> list = null;
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		
		if(resultSet != null){
				try {
					list = new ArrayList<TokenTB>();
					while(resultSet.next()){
						TokenTB obj = new TokenTB();
						
						obj.setId(resultSet.getInt(TokenTB.ID));
						obj.setOpenid(resultSet.getString(TokenTB.OPENID));
						obj.setToken(resultSet.getString(TokenTB.TOKEN));
						obj.setUsername(resultSet.getString(TokenTB.USERNAME));
						
						
					//	purchase.setCnnamediply(dictionaryOS.getENNameByKindAndValue("WAREHOUSE.PURCHASE.STATE", purchase.getState_purchase()).getDictionary_cnname());
						
						list.add(obj);
					}
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
		return list;
	
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean insert(TokenTB token){
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> insertMap = new HashMap<String, Object>();
		insertMap.put(TokenTB.OPENID, token.getOpenid());
		insertMap.put(TokenTB.TOKEN, token.getToken());
		insertMap.put(TokenTB.USERNAME, token.getUsername());
		return sqlMachine.insertRow(TokenTB.TABLE_NAME, insertMap);
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean deleteByOpenid(String openid){
		SqlMachine sqlMachine = new SqlMachine(conn);
		return sqlMachine.deleteRow(TokenTB.TABLE_NAME, TokenTB.OPENID, openid);
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean isExistToken(Cookie[] cookies){
	
		boolean eResult = false;
		
		StringBuffer inStr = new StringBuffer();
		inStr.append("(");
		
		boolean isFirst = true;
		for(Cookie cookie:cookies){
			if(isFirst){
				isFirst = false;
				inStr.append("" + cookie.toString());
			}else{
				inStr.append("," + cookie.toString());
			}
			
		}
		inStr.append(")");
		
		
		
		String sql = "select * from "+TokenTB.TABLE_NAME+" where "+TokenTB.TOKEN+" IN "+inStr;
		
		if(logger.isDebugEnabled()){
			logger.debug("isExist() sql:"+sql.toString());
		}
		try {
			ResultSet result = databaseConnection.executeQuery(conn,sql);
		
			if(result.next()){
				eResult = true;
			}
			result.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return eResult;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public TokenTB getToken(Cookie[] cookies){
	
		TokenTB token = new TokenTB();
		
		StringBuffer inStr = new StringBuffer();
		inStr.append("(");
		
		boolean isFirst = true;
		for(Cookie cookie:cookies){
			if(isFirst){
				isFirst = false;
				inStr.append("'" + cookie.getValue() + "'");
			}else{
				inStr.append(",'" + cookie.getValue() + "'");
			}
			
		}
		inStr.append(")");
		
		
		
		String sql = "select * from "+TokenTB.TABLE_NAME+" where "+TokenTB.TOKEN+" IN "+inStr.toString();
		
		List<TokenTB> list = selectListBySql(sql);
		if(list != null){
			if(list.size() >0){
				token = list.get(0);
			}else{
				token = null;
			}
		}
		
		
		return token;
	}

}
